<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

define( "JIEQI_MODULE_NAME", "system" );
require_once( "../global.php" );
if ( empty( $_REQUEST['mod'] ) )
{
		$GLOBALS['_REQUEST']['mod'] = "system";
}
else if ( !isset( $jieqiModules[$_REQUEST['mod']] ) )
{
		jieqi_printfail( LANG_ERROR_PARAMETER );
}
include_once( JIEQI_ROOT_PATH."/class/power.php" );
$power_handler =& jieqipowerhandler::getinstance( "JieqiPowerHandler" );
$power_handler->getsavedvars( $_REQUEST['mod'] );
jieqi_checkpower( $jieqiPower[$_REQUEST['mod']]['adminconfig'], $jieqiUsersStatus, $jieqiUsersGroup, false, true );
jieqi_loadlang( "configs", JIEQI_MODULE_NAME );
include_once( JIEQI_ROOT_PATH."/class/configs.php" );
$configs_handler =& jieqiconfigshandler::getinstance( "JieqiConfigsHandler" );
$criteria = new criteriacompo( new criteria( "modname", $_REQUEST['mod'], "=" ) );
if ( !isset( $_REQUEST['define'] ) && $_REQUEST['define'] != 1 )
{
		$GLOBALS['_REQUEST']['define'] = 0;
}
$criteria->add( new criteria( "cdefine", $_REQUEST['define'], "=" ) );
$criteria->setsort( "catorder ASC, cid" );
$criteria->setorder( "ASC" );
$configs_handler->queryobjects( $criteria );
$v = $configs_handler->getobject( );
if ( $v )
{
		if ( isset( $_POST['action'] ) && $_POST['action'] == "update" )
		{
				$cfgarray = array( );
				$cfgdefine = "";
				do
				{
						$tmpkey = $v->getvar( "cname", "n" );
						switch ( $v->getvar( "ctype" ) )
						{
						case JIEQI_TYPE_TXTBOX :
						case JIEQI_TYPE_TXTAREA :
						case JIEQI_TYPE_HIDDEN :
								if ( !isset( $_POST[$tmpkey] ) )
								{
										$tmpval = $v->getvar( "cvalue" );
								}
								else
								{
										$tmpval = $_POST[$tmpkey];
								}
								break;
						case JIEQI_TYPE_INT :
								if ( !isset( $_POST[$tmpkey] ) && !is_numeric( $_POST[$tmpkey] ) )
								{
										$tmpval = $v->getvar( "cvalue" );
								}
								else
								{
										$tmpval = $_POST[$tmpkey];
								}
								$tmpval = intval( $tmpval );
								break;
						case JIEQI_TYPE_NUM :
								if ( !isset( $_POST[$tmpkey] ) && !is_numeric( $_POST[$tmpkey] ) )
								{
										$tmpval = $v->getvar( "cvalue" );
								}
								else
								{
										$tmpval = $_POST[$tmpkey];
								}
								break;
						case JIEQI_TYPE_PASSWORD :
								if ( !isset( $_POST[$tmpkey] ) && strlen( $_POST[$tmpkey] ) == 0 )
								{
										$tmpval = $v->getvar( "cvalue" );
								}
								else
								{
										$tmpval = $_POST[$tmpkey];
								}
								break;
						case JIEQI_TYPE_SELECT :
						case JIEQI_TYPE_RADIO :
								$selectary = @unserialize( @$v->getvar( "options", "n" ) );
								if ( !is_array( $selectary ) )
								{
										$selectary = array( );
								}
								if ( isset( $_POST[$tmpkey], $selectary[$_POST[$tmpkey]] ) )
								{
										$tmpval = $v->getvar( "cvalue" );
								}
								else
								{
										$tmpval = $_POST[$tmpkey];
								}
								break;
						case JIEQI_TYPE_MULSELECT :
						case JIEQI_TYPE_CHECKBOX :
								$selectary = @unserialize( @$v->getvar( "options", "n" ) );
								if ( !is_array( $selectary ) )
								{
										$selectary = array( );
								}
								$tmparray = is_array( $_POST[$tmpkey] ) ? $_POST[$tmpkey] : array( );
								$tmpval = 0;
								foreach ( $tmparray as $tmpv )
								{
										if ( isset( $selectary[$tmpv] ) )
										{
												$tmpval |= intval( $tmpv );
										}
								}
								break;
						default :
								if ( !isset( $_POST[$tmpkey] ) )
								{
										$tmpval = $v->getvar( "cvalue" );
								}
								else
								{
										$tmpval = $_POST[$tmpkey];
								}
						}
						if ( $tmpval != $v->getvar( "cvalue", "n" ) )
						{
								$v->setvar( "cvalue", $tmpval );
								$configs_handler->insert( $v );
						}
						if ( $v->getvar( "cdefine" ) == "1" )
						{
								$cfgdefine .= "@define('".$tmpkey."','".jieqi_setslashes( $tmpval, "\"" )."');\n";
						}
						else
						{
								$cfgarray[$_REQUEST['mod']][$tmpkey] = $tmpval;
						}
				} while ( $v = $configs_handler->getobject( ) );
				if ( 0 < count( $cfgarray ) )
				{
						jieqi_setconfigs( "configs", "jieqiConfigs", $cfgarray, $_REQUEST['mod'] );
				}
				if ( !empty( $cfgdefine ) )
				{
						$isdefine = 1;
						$dir = JIEQI_ROOT_PATH."/configs";
						if ( !file_exists( $dir ) )
						{
								@mkdir( $dir, 511 );
						}
						@chmod( $dir, 511 );
						if ( $_REQUEST['mod'] != "system" )
						{
								$dir .= "/".$_REQUEST['mod'];
								if ( !file_exists( $dir ) )
								{
										@mkdir( $dir, 511 );
								}
								@chmod( $dir, 511 );
						}
						$dir .= "/system.php";
						if ( file_exists( $dir ) )
						{
								@chmod( $dir, 511 );
						}
						$cfgdefine = "<?php\n".$cfgdefine."\n?>";
						jieqi_writefile( $dir, $cfgdefine );
						$publicdata = str_replace( "?><?php", "", $cfgdefine.jieqi_readfile( JIEQI_ROOT_PATH."/lang/lang_system.php" ).jieqi_readfile( JIEQI_ROOT_PATH."/configs/groups.php" ) );
						jieqi_writefile( JIEQI_ROOT_PATH."/configs/define.php", $publicdata );
				}
				else
				{
						$isdefine = 0;
				}
				include_once( JIEQI_ROOT_PATH."/class/logs.php" );
				$logs_handler = jieqilogshandler::getinstance( "JieqiLogsHandler" );
				$logdata = array(
						"logtype" => 2,
						"logdata" => "module:".$_REQUEST['mod'].",define:".$isdefine,
						"todata" => serialize( $_REQUEST )
				);
				$logs_handler->addlog( $logdata );
				jieqi_msgwin( LANG_DO_SUCCESS, $jieqiLang['system']['edit_config_success'] );
		}
		else
		{
				include_once( JIEQI_ROOT_PATH."/admin/header.php" );
				include_once( JIEQI_ROOT_PATH."/lib/html/formloader.php" );
				$config_form = new jieqithemeform( $jieqiLang['system']['edit_config'], "config", JIEQI_URL."/admin/configs.php" );
				$catname = "";
				$catorder = 0;
				$catlink = "";
				do
				{
						$tmpvar = $v->getvar( "catname" );
						if ( $catname != $tmpvar )
						{
								$catname = $tmpvar;
								++$catorder;
								${ "catele".$catorder } = new jieqiformlabel( "", "<a name=\"catorder".$catorder."\">".$catname."</a>" );
								$config_form->addelement( ${ "catele".$catorder }, false );
								if ( !empty( $catlink ) )
								{
										$catlink .= "&nbsp;&nbsp;";
								}
								$catlink .= "[<a href=\"#catorder".$catorder."\">".$catname."</a>]";
						}
						switch ( $v->getvar( "ctype" ) )
						{
						case JIEQI_TYPE_INT :
						case JIEQI_TYPE_NUM :
								$tmpvar = $v->getvar( "cname" );
								$$tmpvar = new jieqiformtext( $v->getvar( "ctitle" ), $v->getvar( "cname" ), 25, 100, $v->getvar( "cvalue", "e" ) );
								$$tmpvar->setdescription( $v->getvar( "cdescription" ) );
								$config_form->addelement( $$tmpvar, false );
								break;
						case JIEQI_TYPE_TXTAREA :
								$tmpvar = $v->getvar( "cname" );
								$$tmpvar = new jieqiformtextarea( $v->getvar( "ctitle" ), $v->getvar( "cname" ), $v->getvar( "cvalue", "e" ), 5, 50 );
								$$tmpvar->setdescription( $v->getvar( "cdescription" ) );
								$config_form->addelement( $$tmpvar, false );
								break;
						case JIEQI_TYPE_SELECT :
								$tmpvar = $v->getvar( "cname" );
								$$tmpvar = new jieqiformselect( $v->getvar( "ctitle" ), $v->getvar( "cname" ), $v->getvar( "cvalue", "e" ) );
								$$tmpvar->setdescription( $v->getvar( "cdescription" ) );
								$selectary = @unserialize( @$v->getvar( "options", "n" ) );
								if ( !is_array( $selectary ) )
								{
										$selectary = array( );
								}
								foreach ( $selectary as $val => $cap )
								{
										$$tmpvar->addoption( $val, $cap );
								}
								$config_form->addelement( $$tmpvar, false );
								break;
						case JIEQI_TYPE_RADIO :
								$tmpvar = $v->getvar( "cname" );
								$$tmpvar = new jieqiformradio( $v->getvar( "ctitle" ), $v->getvar( "cname" ), $v->getvar( "cvalue", "e" ) );
								$$tmpvar->setdescription( $v->getvar( "cdescription" ) );
								$selectary = @unserialize( @$v->getvar( "options", "n" ) );
								if ( !is_array( $selectary ) )
								{
										$selectary = array( );
								}
								foreach ( $selectary as $val => $cap )
								{
										$$tmpvar->addoption( $val, $cap );
								}
								$config_form->addelement( $$tmpvar, false );
								break;
						case JIEQI_TYPE_CHECKBOX :
								$tmpvar = $v->getvar( "cname" );
								$tmpvalue = decbin( intval( $v->getvar( "cvalue", "n" ) ) );
								$tmplen = strlen( $tmpvalue );
								$tmparray = array( );
								$tmpnum = 1;
								$p = $tmplen - 1;
								for ( ;	0 <= $p;	--$p	)
								{
										if ( $tmpvalue[$p] == "1" )
										{
												$tmparray[] = $tmpnum;
										}
										$tmpnum *= 2;
								}
								$$tmpvar = new jieqiformcheckbox( $v->getvar( "ctitle" ), $v->getvar( "cname" ), $tmparray );
								$$tmpvar->setdescription( $v->getvar( "cdescription" ) );
								$selectary = @unserialize( @$v->getvar( "options", "n" ) );
								if ( !is_array( $selectary ) )
								{
										$selectary = array( );
								}
								foreach ( $selectary as $val => $cap )
								{
										$$tmpvar->addoption( $val, $cap );
								}
								$config_form->addelement( $$tmpvar, false );
								break;
						case JIEQI_TYPE_LABEL :
								$tmpvar = $v->getvar( "cname" );
								$$tmpvar = new jieqiformlabel( $v->getvar( "ctitle" ), $v->getvar( "cvalue" ) );
								$$tmpvar->setdescription( $v->getvar( "cdescription" ) );
								$config_form->addelement( $$tmpvar, false );
								break;
						case JIEQI_TYPE_PASSWORD :
								$tmpvar = $v->getvar( "cname" );
								$$tmpvar = new jieqiformpassword( $v->getvar( "ctitle" ), $v->getvar( "cname" ), 25, 30, "" );
								$$tmpvar->setdescription( $v->getvar( "cdescription" ) );
								$config_form->addelement( $$tmpvar, false );
								break;
						case JIEQI_TYPE_TXTBOX :
								$tmpvar = $v->getvar( "cname" );
								$$tmpvar = new jieqiformtext( $v->getvar( "ctitle" ), $v->getvar( "cname" ), 25, 100, $v->getvar( "cvalue", "e" ) );
								$$tmpvar->setdescription( $v->getvar( "cdescription" ) );
								$config_form->addelement( $$tmpvar, false );
						}
				} while ( $v = $configs_handler->getobject( ) );
				$config_form->addelement( new jieqiformhidden( "mod", $_REQUEST['mod'] ) );
				$config_form->addelement( new jieqiformhidden( "define", $_REQUEST['define'] ) );
				$config_form->addelement( new jieqiformhidden( "action", "update" ) );
				$config_form->addelement( new jieqiformbutton( "&nbsp;", "submit", $jieqiLang['system']['save_config'], "submit" ) );
				$jieqiTpl->assign( "jieqi_contents", "<div style=\"text-align:center;\"><span style=\"line-height:200%\">".$catlink."</span></div>".$config_form->render( JIEQI_FORM_MIDDLE )."<br />" );
				include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
		}
}
else
{
		jieqi_msgwin( LANG_NOTICE, $jieqiLang['system']['no_usage_config'] );
}
?>
